package dao

import (
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/request"

	"gorm.io/gorm"
)

type GetRegion struct{}

func (g *GetRegion) Name(name string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if name == "" {
			return db
		} else {
			return db.Where("hft_regions.name LIKE ?", fmt.Sprintf("%v%%", name))
		}
	}
}

func (g *GetRegion) AccountRegion(link string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("hft_regions.link LIKE ?", fmt.Sprintf("%v%%", link))
	}
}

func (g *GetRegion) PID(belong int, pid int, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if pid == 0 {
			return db.Where("hft_regions.p_id = ?", belong)
		} else {
			return db.Where("hft_regions.p_id = ?", pid)
		}
	}
}

func (g *GetRegion) Join(db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Select("hft_regions.*,a.id AS user_id, a.username,a.phone,a.real_name").Joins("LEFT JOIN hft_accounts a ON a.id = hft_regions.manage_id")
	}
}

func (g *GetRegion) GetRegionList(account *admin.Account, paginate *request.RegionInfoList, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Scopes(g.Join(db)).Scopes(g.Name(paginate.Name, db)).Scopes(g.PID(int(account.Belong), int(paginate.ID), db)).Scopes(g.AccountRegion(account.RegionLink, db)).Scopes(Paginate(Page(paginate.Page), db)).Order("sort desc,update_time desc")
	}
	// var searchString = "create_time,update_time,id,center,level,name,p_id,sort,can_add_child,first_word,is_hot,pre_fix,is_default,alias"
	// if PID == 0 {
	// 	if !strings.Contains(account.RegionLink, "|") {
	// 		return func(db *gorm.DB) *gorm.DB {
	// 			return db.Where("link = ?", account.RegionLink).Where("name LIKE ?", fmt.Sprintf("%%%v%%", paginate.Name)).Scopes(Paginate(Page(paginate.Page), db)).Select(searchString)
	// 		}
	// 	} else {
	// 		return func(db *gorm.DB) *gorm.DB {
	// 			return db.Where("manage_id = ?", account.ID).Where("name LIKE ?", fmt.Sprintf("%%%v%%", paginate.Name)).Scopes(Paginate(Page(paginate.Page), db)).Select(searchString)
	// 		}
	// 	}
	// } else {
	// 	if !strings.Contains(account.RegionLink, "|") {
	// 		return func(db *gorm.DB) *gorm.DB {
	// 			return db.Where("link LIKE ? AND p_id = ?", fmt.Sprintf("%v%%", account.RegionLink), PID).Where("name LIKE ?", fmt.Sprintf("%%%v%%", paginate.Name)).Scopes(Paginate(Page(paginate.Page), db)).Select(searchString)
	// 		}
	// 	} else {
	// 		return func(db *gorm.DB) *gorm.DB {
	// 			return db.Where("link REGEXP ? AND p_id = ?", fmt.Sprintf("(^%v)", account.RegionLink), PID).Where("name LIKE ?", fmt.Sprintf("%%%v%%", paginate.Name)).Scopes(Paginate(Page(paginate.Page), db)).Select(searchString)
	// 		}
	// 	}
	// }

}

func (g *GetRegion) ID(id int, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if id == 0 {
			return db
		} else {
			return db.Where("p_id = ?", id)
		}
	}
}

func (g *GetRegion) List(paginate *request.RegionInfoList, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Scopes(g.Name(paginate.Name, db)).Scopes(g.ID(int(paginate.ID), db)).Scopes(Paginate(Page(paginate.Page), db))
	}
}

var GetRegionDao = new(GetRegion)
